package com.czd.support.controller;


import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czd.base.model.JsonResult;
import com.czd.base.model.Paginate;
import com.czd.base.utils.WebUtils;
import com.czd.support.common.Configuration;
import com.czd.support.enums.AuditEnum;
import com.czd.support.model.AgentAttachment;
import com.czd.support.model.Certification;
import com.czd.support.model.Manager;
import com.czd.support.model.Reason;
import com.czd.support.model.User;
import com.czd.support.model.UserCompany;
import com.czd.support.service.AgentAttachmentService;
import com.czd.support.service.AssetService;
import com.czd.support.service.CertificationService;
import com.czd.support.service.ReasonService;
import com.czd.support.service.UserCompanyService;
import com.czd.support.service.UserService;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (2014/9/14 22:24)
 * Description: //TODO 文件描述
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/support")
public class SupportController extends BaseController {

	@Autowired
	private UserService userService;
	@Autowired
	private CertificationService certificationService;
	@Autowired
	private AssetService assetService;
	@Autowired
	private UserCompanyService userCompanyService;
	@Autowired
	private ReasonService reasonService;
  
	@Autowired
	private AgentAttachmentService  agentAttachmentService  ;
	@RequestMapping(value = {"","/"})
	public String index(HttpServletRequest request){
		Manager manager = this.getSesionUser(request);
		if(manager != null ){
			return "index";
		}else{
			return "redirect:/login";
		}
	}

	/**
	 * 身份认证列表拦截
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = {"/{cStatus}/authentication"},method = RequestMethod.GET)
	public String authentication(Model model,HttpServletRequest request,@PathVariable int cStatus, Certification certification,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
		Manager manager = this.getSesionUser(request);
		certification.setStatus(cStatus);
		if(manager != null ){
            certification.setPageSize(Configuration.getInstance().getDefault_page_size());
            certification.setPageNo(pageNo);
            String kw = WebUtils.findParamStr(request,"kw");
			Paginate paginate  = userService.queryForPage(certification,kw);
			model.addAttribute("paginate",paginate);
			return "support/authentication";
		}else{
			return "redirect:/login";
		}
	}

	/**
	 * 身份认证详情页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = {"/authentication/{id}"},method = RequestMethod.GET)
	public String view(Model model,HttpServletRequest request,@PathVariable int id){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			model.addAttribute("employee",userService.findById(id));
			return "support/details";
		}else{
			return "redirect:/login";
		}
	}

	/**
	 * 身份认证审核
	 * @param request
	 * @return
	 */
	@RequestMapping(value = {"/authentication/{id}/audit"},method = RequestMethod.POST)
	@ResponseBody
	public JsonResult audit(HttpServletRequest request,@PathVariable int id){
		Manager manager = this.getSesionUser(request);
		boolean isAudit = WebUtils.findParamBoolean(request,"isAudit");
		if(manager != null){
			Certification certification = new Certification();
			certification.setUserId(id);
			certification.setUpdateUserId(manager.getUserCode());
			if(isAudit) {
				certification.setcStatus(AuditEnum.审核通过.ordinal());
			}else {
				certification.setcStatus(AuditEnum.审核未通过.ordinal());
			}
			if(certificationService.audit(certification)){
				if(isAudit) {
					assetService.insert(id);
				}
				return JsonResult.OK;
			}else {
				return JsonResult.FAIL.message("审核通过失败");
			}
		}else{
			return JsonResult.FAIL;
		}
	}

	/**
	 * 代理商客户列表
	 * @param model
	 * @param request
	 * @param user
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = {"/userList"},method = RequestMethod.GET)
	public String userList(Model model,HttpServletRequest request,User user,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
		Manager manager = this.getSesionUser(request);
		if(manager != null ){
			user.setPageSize(Configuration.getInstance().getDefault_page_size());
			user.setPageNo(pageNo);
            String kw = WebUtils.findParamStr(request,"kw");
            if(StringUtils.isNotBlank(kw)){
            	User proxyUser = userService.findByKw(user, kw);
            	model.addAttribute("proxyUser", proxyUser);
            }
			Paginate paginate = userService.queryPageForUser(user,kw);
			model.addAttribute("paginate",paginate);
			return "support/userlist";
		}else{
			return "redirect:/login";
		}
	}
	
	/**
	 * 代理商列表
	 * @param model
	 * @param request
	 * @param user
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = {"/{auditStatus}/proxyList"},method = RequestMethod.GET)
	public String proxyList(@PathVariable int auditStatus, Model model,HttpServletRequest request,UserCompany userCompany,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
		Manager manager = this.getSesionUser(request);
		if(manager != null ){
			userCompany.setPageSize(Configuration.getInstance().getDefault_page_size());
			userCompany.setPageNo(pageNo);
            String kw = WebUtils.findParamStr(request,"kw");
			Paginate paginate = userCompanyService.queryPageForProxy(userCompany,kw,auditStatus);
			model.addAttribute("paginate",paginate);
			model.addAttribute("auditStatus",auditStatus);
			return "support/proxylist";
		}else{
			return "redirect:/login";
		}
	}
	
	/**
	 * 申请代理详情
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = {"/proxyView/{id}"},method = RequestMethod.GET)
	public String proxyView(Model model,HttpServletRequest request,@PathVariable int id){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			model.addAttribute("userCompany",userCompanyService.findProxyDetails(id));
			return "support/proxydetails";
		}else{
			return "redirect:/login";
		}
	}
	
	/**
	 * 审核通过
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/updateAuditStatus/{auditStatus}/{userId}"},method = RequestMethod.GET)
	public String updateAuditStatus(@PathVariable int auditStatus, @PathVariable int userId, Model model){
		UserCompany userCompany = userCompanyService.findByUserId(userId);
		userCompany.setAuditStatus(auditStatus);
		if(userCompanyService.updateAuditStatus(userCompany)){
			Certification certification = certificationService.findByUserId(userCompany.getUserId());
			if(certification == null){
				certification = new Certification();
				certification.setUserId(userCompany.getUserId());
				certification.setRealName(userCompany.getName());
				certification.setPositiveCopies(userCompany.getPositiveCopies());
				certification.setNegativeCopies(userCompany.getNegativeCopies());
				certification.setcStatus(1);
				certification.setCreateUserId(userCompany.getUserId());
				certificationService.insert(certification);
			}else{
				if(certification.getcStatus() != 1){
					certification.setPositiveCopies(userCompany.getPositiveCopies());
					certification.setNegativeCopies(userCompany.getNegativeCopies());
					certification.setcStatus(1);
					certificationService.update(certification);
				}
			}
			return "support/proxylist";
		}
		return error(model, "提示信息", "操作失败！");
	}
	
	/**
	 * 审核不通过
	 * @param model
	 * @param request
	 * @param reason
	 * @return
	 */
	@RequestMapping(value="/disAgreeAply", method = RequestMethod.POST)
	public String disAgreeAply(Model model,HttpServletRequest request,Reason reason){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			int userCompanyId = Integer.parseInt(request.getParameter("userCompanyId"));
			Reason reasonIsExist = reasonService.findByUserCompanyId(userCompanyId);
			UserCompany userCompany = userCompanyService.findById(userCompanyId);
			userCompany.setAuditStatus(2);
			if(reasonIsExist == null){
				if(reasonService.insert(reason)){
					userCompanyService.updateAuditStatus(userCompany);
					return "support/proxylist";
				}
			}else{
				reasonIsExist.setReasonContent(reason.getReasonContent());
				if(reasonService.update(reasonIsExist)){
					userCompanyService.updateAuditStatus(userCompany);
					return "support/proxylist";
				}
			}
			return error(model, "提示信息", "提交失败！");
		}else{
			return "redirect:/login";
		}
	}
	
	
	/**
	 * 增加备注
	 * @author jinma .xiao
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = {"/beizhu"},method = RequestMethod.POST)
	public String beizhu(Model model,HttpServletRequest request,UserCompany  userCompany){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			String userCompanyid = request.getParameter("userCompanyid");
			String neirong = request.getParameter("neirong");
			//t_user_company 
			userCompany.setRemark(neirong);
			userCompany.setUserId(Integer.parseInt(userCompanyid));  
			userCompanyService.addRemark(userCompany);
			return "redirect:/support/-1/proxyList";
		}else{
			return "redirect:/login";
		}
	}
	
	
	/**
	 * 代理附件
	 * @param talk
	 * @param request
	 * @param model
	 * @param pageNo
	 * @return  
	 */
	@RequestMapping(value = "/agentlist", method = RequestMethod.GET)
	public String list(AgentAttachment agentAttachment, HttpServletRequest request, Model model,
			@RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		Manager manager = this.getSesionUser(request);
		if (manager != null) {
			agentAttachment.setPageSize(Configuration.getInstance().getDefault_page_size());
			agentAttachment.setPageNo(pageNo);
			String kw = WebUtils.findParamStr(request, "kw");
	 		Paginate queryForPaginate = agentAttachmentService.queryForPaginate(agentAttachment,kw);
			model.addAttribute("paginate", queryForPaginate);
			return "support/agentlist";
			
		} else {
			return "redirect:/login";
		}
	}
	
	/**
	 * 代理附件  明细
	 * @param talk
	 * @param request
	 * @param model
	 * @param pageNo
	 * @return  
	 */
	@RequestMapping(value = "/agent/{id}/detail", method = RequestMethod.GET)
	public String agentldetail(AgentAttachment agentAttachment, HttpServletRequest request, Model model, @PathVariable int id ) {
		Manager manager = this.getSesionUser(request);
		if (manager != null) {
			AgentAttachment findDtail = agentAttachmentService.findDtail(id);
			model.addAttribute("findDtail", findDtail);
			return "support/agentDetail";
			
		} else {  
			return "redirect:/login";
		}
	}

	/**
	 *  
	 * @author jinma .xiao
	 * @param model
	 * @param request
	 * @return  ////attach/adver/201507/113518745_12703050_980x1200_0 (1).jpg
	 * @throws Exception 
	 */  
	@RequestMapping(value = {"/agent/addagent"},method = RequestMethod.POST)
	public String addagent(Model model,HttpServletRequest request,AgentAttachment  agentAttachment) throws Exception{
		Manager manager = this.getSesionUser(request);
		if(manager != null){ 
			String fileUrl = agentAttachment.getFileUrl(); //文件路径
			agentAttachment.setFileName(fileUrl.substring(fileUrl.lastIndexOf("/")+1, fileUrl.lastIndexOf(".")));//文件名
			agentAttachment.setCreateUser(manager.getUserCode());//创建者
			agentAttachmentService.addAgent(agentAttachment);
			return "redirect:/support/agentlist";      
		}else{ 
			return "redirect:/login";
		}
	}
	
	/**
	 * 附件上传页面跳转
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/toAgentAdd", method = RequestMethod.GET)
	public String toAgentAdd(HttpServletRequest request){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			return "support/addagent";
		}else{
			return "redirect:/login";
		}
	}
	
	/**
	 * 上传代理附件
	 * @param request
	 * @param agentAttachment
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/agentUpload", method = RequestMethod.POST)
	public String agentUpload(HttpServletRequest request, AgentAttachment agentAttachment, Model model){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			agentAttachment.setCreateUser(manager.getUserCode());
			agentAttachment.setUpdateUser(manager.getUserCode());
			if(agentAttachmentService.insert(agentAttachment)){
				return "redirect:/support/agentlist";   
			}
			return error(model, "提示", "上传附件失败！");
		}else{
			return "redirect:/login";
		}
	}
	
	/**
	 * 代理附件编辑页面跳转
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/{id}/toAgentEdit", method = RequestMethod.GET)
	public String toAgentEdit(HttpServletRequest request, @PathVariable int id, Model model){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			AgentAttachment agentAttachment = agentAttachmentService.findDtail(id);
			model.addAttribute("agentAttachment", agentAttachment);
			return "support/editagent";
		}else{
			return "redirect:/login";
		}
	}
	
	/**
	 * 编辑代理附件
	 * @param request
	 * @param agentAttachment
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/editAgentAttachment", method = RequestMethod.POST)
	public String  editAgentAttachment(HttpServletRequest request, AgentAttachment agentAttachment, Model model){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			agentAttachment.setUpdateUser(manager.getUserCode());
			if(agentAttachmentService.update(agentAttachment)){
				return "redirect:/support/agentlist";   
			}
			return error(model, "提示", "上传附件失败！");
		}else{
			return "redirect:/login";
		}
	}
	
	/**
	 * 代理附件删除
	 * @param request
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/agentDelete", method = RequestMethod.GET)
	public String agentDelete(HttpServletRequest request, @PathVariable int id, Model model){
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			if(agentAttachmentService.delete(id)){
				return "redirect:/support/agentlist";   
			}
			return error(model, "提示", "附件删除失败！");
		}else{
			return "redirect:/login";
		}
	}
}
